#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int n = stones.size();
        int sum = 0;
        for (auto e : stones) sum += e;
        int aim = sum / 2;
        vector<int>dp(aim + 1);
        for (int i = 0; i < n; i++)
        {
            for (int j = aim; j >= stones[i]; j--)
            {
                dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - 2 * dp[aim];
    }
};